//
//  FindPwdViewController.m
//  FD
//
//  Created by Yasin on 2018/10/12.
//  Copyright © 2018年 ZhiYuan Network. All rights reserved.
//


#define kTFViewHeight 50
#import "ForgetPwdViewController.h"
#import "FindPwdViewController.h"

//view
#import "FDSameToAndroidTextFieldCustomView.h"//1-20
#import "RBTextField.h"
#import "FDSendVerifyCodeButton.h" //倒计时验证码

@interface ForgetPwdViewController ()

@property (nonatomic, strong) FDSameToAndroidTextFieldCustomView *emailTFView;
@property (nonatomic, strong) FDSameToAndroidTextFieldCustomView *codeTFView;
/**下一步按钮*/
@property (nonatomic, strong) UIButton *nextStepBtn;
/**记录token*/
@property (nonatomic, copy) NSString *fwqToken;

@end

@implementation ForgetPwdViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    [self setNavAndBgc];
    
    [self __setupChildView];
    
//    [self defautlSetlValue];
}

- (void)setNavAndBgc {
    //返回箭头
    UIBarButtonItem *barItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"返回箭头"] style:(UIBarButtonItemStylePlain) target:self action:@selector(back)];
    barItem.tintColor = [UIColor blackColor]; //取消默认蓝色渲染色
    self.navigationItem.leftBarButtonItem = barItem;
    
    NSString *forgetPwdStr = NSLocalizedString(ForgetPassword, @"忘记密码");
    
    self.navigationItem.title =forgetPwdStr;
    self.view.backgroundColor = [UIColor whiteColor];
}

- (void)back {
    [self dismissViewControllerAnimated:YES completion:nil];
}

- (void)__setupChildView {
    
    [self.view addSubview:self.emailTFView];
    [self.view addSubview:self.codeTFView];
    [self.view addSubview:self.nextStepBtn];
    
    
    [self.emailTFView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view.mas_left);
        make.right.equalTo(self.view.mas_right);
        make.top.equalTo(self.view.mas_top).offset(200);
        make.height.mas_equalTo(kTFViewHeight);
    }];
    
    [self.codeTFView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view.mas_left);
        make.right.equalTo(self.view.mas_right);
        make.top.equalTo(self.emailTFView.mas_bottom).offset(10);
        make.height.mas_equalTo(kTFViewHeight);
    }];
    
    [self.nextStepBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.codeTFView.mas_bottom).offset(40);
        make.left.equalTo(self.view.mas_left).offset(15);
        make.right.equalTo(self.view.mas_right).offset(-15);
        make.height.mas_equalTo(44);
    }];
}


#pragma mark - geter
- (FDSameToAndroidTextFieldCustomView *)emailTFView {
    if (!_emailTFView) {
        
        
        _emailTFView = [[FDSameToAndroidTextFieldCustomView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, kTFViewHeight) title:NSLocalizedString(Put_your_email, @"请输入邮箱")];
        //键盘样式：邮箱
        _emailTFView.tf.keyboardType = UIKeyboardTypeEmailAddress;
        
        //添加右侧按钮发送验证码
        FDSendVerifyCodeButton *btn = [[FDSendVerifyCodeButton alloc] initWithFrame:CGRectMake(0, 0, 100, 40)];
        [_emailTFView.topBgView addSubview:btn];
        [btn addTarget:self action:@selector(clickSendCodeAction:) forControlEvents:(UIControlEventTouchUpInside)];
        [btn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.right.bottom.equalTo(self->_emailTFView.topBgView);
            make.width.mas_greaterThanOrEqualTo(100);
        }];
    }
    return _emailTFView;
}


- (FDSameToAndroidTextFieldCustomView *)codeTFView {
    if (!_codeTFView) {
        _codeTFView = [[FDSameToAndroidTextFieldCustomView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, kTFViewHeight) title:NSLocalizedString(Put_your_verifyCode, @"请输入验证码")];
    }
    return _codeTFView;
}

//
//- (RBTextField *)codeTF {
//    if (!_codeTF) {
//        _codeTF = [[RBTextField alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 40)];
//
//        _codeTF.placeholder = NSLocalizedString(Put_your_verifyCode, @"请输入验证码");
//
//        //睁眼闭眼按钮
//        UIButton *openCloseBtn = [UIButton buttonWithType:(UIButtonTypeCustom)];
//        openCloseBtn.frame = CGRectMake(0, 0, 30, 20);
//        [openCloseBtn setImage:[UIImage imageNamed:@"by"] forState:(UIControlStateNormal)];
//        [openCloseBtn setImage:[UIImage imageNamed:@"zy"] forState:(UIControlStateSelected)];
//        [openCloseBtn addTarget:self action:@selector(clickOpenCloseBtnAction:) forControlEvents:(UIControlEventTouchUpInside)];
//        self.codeTF.rightView = openCloseBtn;
//        self.codeTF.rightViewMode = UITextFieldViewModeAlways;
//    }
//    return _codeTF;
//}


//h:44  弧度:4
- (UIButton *)nextStepBtn {
    if (!_nextStepBtn) {
        NSString *nextStr = NSLocalizedString(Next, @"下一步");
        _nextStepBtn = [UIButton createFastRedBtnRadius:0 target:self action:@selector(clickNextBtnAction:) title:nextStr];
        
        _nextStepBtn.layer.cornerRadius = 4;
        _nextStepBtn.layer.masksToBounds = YES;
    }
    return _nextStepBtn;
}

#pragma mark - 点击事件

//:!!!!睁眼闭眼按钮
//- (void)clickOpenCloseBtnAction:(UIButton *)button {
//    button.selected = !button.selected;
//
//    if (button.selected) {
//        self.codeTF.secureTextEntry = NO;
//    }else {
//        self.codeTF.secureTextEntry = YES;
//    }
//}

//:!!!!发送验证码
- (void)clickSendCodeAction:(FDSendVerifyCodeButton *)btn {
    
    //回收键盘
    [self.view endEditing:YES];
    
    NSString *emailStr = self.emailTFView.tf.text;
    
    if ([NSString isEmptyString:emailStr]) {
        [JRToast showWithText:NSLocalizedString(Put_your_email, @"请输入邮箱")];
        return;
    }

    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    
    [FDHttpAPi requestUserFindPWdSendCodeWithEmail:emailStr successBlock:^(id responseObjc) {
        
        [hud hideAnimated:YES];
        
        NSString *event = responseObjc[@"event"];
        if ([event isEqualToString:SUCCESS]) {
            [JRToast showWithText:NSLocalizedString(ToastVerifyCodeAlreadySent, @"验证码已发送")];
            NSString *token = responseObjc[@"data"][@"token"];
            self.fwqToken = token;
        
            [btn verifyCodeCountDownTime:60]; //1-7开启验证码倒计时
        }else {
            [JRToast showWithText:responseObjc[describe]];
        }
        
    } failBlock:^(NSError *error) {
        FDLog(@"发生错误了:%@",[error localizedDescription]);
        [hud hideAnimated:YES];
    }];
}

//!!!!:下一步
- (void)clickNextBtnAction:(UIButton *)btn
{
    
    [self.view endEditing:YES];
    
    NSString *emailStr = self.emailTFView.tf.text;
    NSString *codeStr = self.codeTFView.tf.text;
    
    if ([NSString isEmptyString:emailStr]) {
        [JRToast showWithText:NSLocalizedString(Put_your_email, @"请输入邮箱")];
        return;
    }
    if ([NSString isEmptyString:codeStr]) {
        [JRToast showWithText:NSLocalizedString(Put_your_verifyCode, @"请输入验证码")];
        return;
    }
    
    if ([NSString isEmptyString:self.fwqToken]) {
        FDLog(@"没有调用发送验证码接口");
        [JRToast showWithText:NSLocalizedString(PleaseGetEmailCodeFirst, @"请获取邮箱验证码")];
        return;//
    }
    
    FindPwdViewController *findPwdVC2 = [[FindPwdViewController alloc] init];
    
    //token
    findPwdVC2.token = self.fwqToken;
    findPwdVC2.email = emailStr;
    findPwdVC2.code = codeStr;
    
    [self.navigationController pushViewController:findPwdVC2 animated:YES];
}


#pragma mark - 回收键盘
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    [self.view endEditing:YES];
}
@end

